package org.jetnuke.modules.pictures;

import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.entities.Theme;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;

public class PreviewTheme implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(PreviewTheme.class);

	int theme;

	public void setTheme(int theme) {
		this.theme = theme;
	}

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		String _styleFolder = "_styleFolder";
		String currentStyle = null;
		try {
			Theme tm = net.jforum.dao.DataAccessDriver.getInstance()
					.newThemeDAO().selectByID(theme);
			currentStyle = tm.getStyleName();
		} catch (Throwable e) {
			logger
					.warn(
							"exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - exception ignored", e); //$NON-NLS-1$

		}

		int id = ui.getID();

		User userAction = new User();
		userAction.setId(id);
		ActionForward af = userAction.exec(request, response, vc, info, op, ui,
				ai);
		if (currentStyle != null)
			vc.put(_styleFolder, "style/" + currentStyle);
		vc.put("_currentStyle", currentStyle);

		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return af;
		// return new ActionForward(op,info);
	}

}
